﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class FixedAngleSpring : Spring
    {
        private FarseerGames.FarseerPhysics.Dynamics.Springs.FixedAngleSpring mFixedAngleSpring = null;
        
        /// <summary>
        /// Gets or sets the target angle in degrees that the spring will be try to stay at.
        /// </summary>
        public Single TargetAngle { get { return MathHelper.ToDegrees(mFixedAngleSpring.TargetAngle); } set { mFixedAngleSpring.TargetAngle = MathHelper.ToRadians(value); } }

        /// <summary>
        /// Gets or sets the target angle in radians that the spring will be try to stay at.
        /// </summary>
        public Single TargetAngleRadians { get { return mFixedAngleSpring.TargetAngle; } set { mFixedAngleSpring.TargetAngle = value; } }

        /// <summary>
        /// Gets or sets the maximum amount of torque that will be applied when attempting to reach the TargetAngle.
        /// </summary>
        public Single MaxTorque { get { return mFixedAngleSpring.MaxTorque; } set { mFixedAngleSpring.MaxTorque = value; } }

        /// <summary>
        /// Gets or sets the amount the Torque is multiplied by, set to 1.0 to not multiply the Torque at all.
        /// </summary>
        public Single TorqueMultiplier { get { return mFixedAngleSpring.TorqueMultiplier; } set { mFixedAngleSpring.TorqueMultiplier = value; } }

        /// <summary>
        /// Gets or sets the pull/push of the spring. Think of this as the force of the spring.
        /// </summary>
        public Single SpringConstant { get { return mFixedAngleSpring.SpringConstant; } set { mFixedAngleSpring.SpringConstant = value; } }

        /// <summary>
        /// Gets or sets the dampening of the spring. Think of this as a shock absorber.
        /// </summary>
        public Single DampingConstant { get { return mFixedAngleSpring.DampingConstant; } set { mFixedAngleSpring.DampingConstant = value; } }

        /// <summary>
        /// Defines the maximum allowed value that SpringError can reach before the spring is broken.
        /// </summary>
        public Single Breakpoint { get { return mFixedAngleSpring.Breakpoint; } set { mFixedAngleSpring.Breakpoint = value; } }

        /// <summary>
        /// Creates a new instance of an FixedAngleSpring component.
        /// </summary>
        /// <param name="gameObject">The GameObject this Component is attached to.</param>
        public FixedAngleSpring(GameObject gameObject)
            : base(gameObject)
        {
            mFixedAngleSpring = new FarseerGames.FarseerPhysics.Dynamics.Springs.FixedAngleSpring();
            mSpring = mFixedAngleSpring;
        }

        /// <summary>
        /// Create the FixedAngleSpring, call this after setting any initial values.
        /// </summary>
        public override void Create()
        {
            mFixedAngleSpring = SpringFactory.Instance.CreateFixedAngleSpring(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, SpringConstant, DampingConstant);
            mSpring = mFixedAngleSpring;
        }
    }
}
